/**
 * Created by Administrator on 2017/5/9.
 */
const mongoose = require('mongoose');
const setting = require('../setting');
mongoose.Promise = global.Promise;
mongoose.connect(`mongodb://${setting.host}/${setting.db}`);
const DbSet = {
    addOne:(obj,req,res,logMsg)=>{
        let newobj = new (obj.body);
        newobj.save().then(result =>{
            res.end(logMsg)
        }).catch(err=>{
            res.end(err)
        })
    },
    delOne:(obj,req,res,logMsg)=>{
        let params = url.parse(req,url,true);
        let targetId = params.query.id;
        obj.remove({_id:targetId}).then(result =>{
            res.end(logMsg)
        }).catch(err=>{
            res.end(err)
        })
    },
    updateOne:(obj,req,res,logMsg)=>{
        let params = url.parse(req,url,true);
        let targetId = params.query.id;
        obj.update({_id:targetId}).then(result =>{
            res.end(logMsg)
        }).catch(err=>{
            res.end(err)
        })
    },
    findOne:(obj,req,res,logMsg)=>{
        let params = url.parse(req,url,true);
        let targetId = params.query.id;
        obj.findOne({_id:targetId}).then(result =>{
            res.end(logMsg)
        }).catch(err=>{
            res.end(err)
        })
    },
    findAll:(obj,req,res,logMsg)=>{

        obj.find({}).then(result =>{
            res.end(logMsg)
        }).catch(err=>{
            res.end(err)
        })
    },
}
module.exports = DbSet;